package utils

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

var lRand = rand.New(rand.NewSource(time.Now().UnixNano()))

// 格式化脱敏email
func FormatEmail(emailOrigin string) string {
	if len(emailOrigin) <= 0 {
		return ""
	}
	email := ""
	if len(emailOrigin) > 0 && strings.Contains(emailOrigin, "@") {
		index := strings.IndexByte(emailOrigin, '@')
		if index > 2 {
			email = emailOrigin[:1] + "***" + emailOrigin[index-1:index] + emailOrigin[index:]
		} else {
			email = "***" + emailOrigin[index:]
		}
	}
	return email
}

// 格式化脱敏账号名
func FormatAccountByPhone(phone int64) string {
	if phone < 0 {
		return strconv.FormatInt(phone, 10)
	}
	allChar := "kadcmeofhbisjlqnpyrtugvwzx"
	p := strconv.FormatInt(phone, 10)
	index := lRand.Intn(21)
	allChar = allChar[index : index+4]
	return p[len(p)-4:] + allChar
}

// 格式化脱敏账号名
func FormatAccountByName(name string) string {
	if name == "" {
		return name
	}
	allChar := "kadcmeofhbisjlqnpyrtugvwzx"
	index := lRand.Intn(21)
	allChar = allChar[index : index+4]
	userName := ""
	for i, n := range name {
		if i < 4 {
			userName += string(n)
		} else {
			break
		}
	}
	userName += allChar
	return userName
}

// 格式化脱敏账号名
func FormatAccountByEmail(emailOrigin string) string {
	if len(emailOrigin) <= 0 {
		return ""
	}
	email := ""
	if len(emailOrigin) > 0 && strings.Contains(emailOrigin, "@") {
		index := strings.IndexByte(emailOrigin, '@')
		if index > 3 {
			email = emailOrigin[index-4 : index]
		} else {
			email = emailOrigin[:index]
		}
	}
	allChar := "kadcmeofhbisjlqnpyrtugvwzx"
	index := lRand.Intn(21)
	allChar = allChar[index : index+4]
	return email + allChar
}
